package com.vcc.core.fragment;

import com.vcc.core.util.AesUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AESEncryptHandler extends BaseTypeHandler<Object> {

    private static final Logger logger = LoggerFactory.getLogger(AESEncryptHandler.class);

    @Override
    public void setNonNullParameter( PreparedStatement ps, int i, Object parameter,  JdbcType jdbcType) {

        try {
            ps.setString(i, parameter == null ? "" : (parameter.toString()));
        } catch (Exception var9) {
            logger.error("typeHandler加密异常：" + var9);
        }

    }



    @Override
    public Object getNullableResult( ResultSet rs,  String columnName) {
        try {
            String tempString = rs.getString(columnName);
            if (tempString == null) {
                return null;
            } else {
                return AesUtil.decrypt(tempString);
            }
        } catch (SQLException e) {
            return "";
        }
    }

    @Override
    public Object getNullableResult( ResultSet rs, int columnIndex) {
        try {
            String tempString =  rs.getString(columnIndex);
            if (tempString == null) {
                return null;
            } else {
                return AesUtil.decrypt(tempString);
            }
        } catch (SQLException e) {
            return "";
        }
    }

    @Override
    public Object getNullableResult( CallableStatement cs, int columnIndex) {
        try {
            String tempString =  cs.getString(columnIndex);
            if (tempString == null) {
                return null;
            } else {
                return AesUtil.decrypt(tempString);
            }
        } catch (SQLException e) {
            return "";
        }
    }
}
